/**
 * 
 */
package com.thor.palmmall.rest.vkcommunicate;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.vkcommunicate.converter.AVKCommunicateConverter;
import com.thor.palmmall.rest.vkcommunicate.converter.SVKCommunicateConverter;
import com.thor.palmmall.rest.vkcommunicate.converter.SVKCommunicateQueryParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.vkcommunicate.SVKCommunicate;
import com.thor.palmmall.spi.vkcommunicate.SVKCommunicateService;

/**
 * @author Captain
 *
 * @since 1.0
 */
public class AVKCommunicateServiceImpl implements AVKCommunicateService {

  @Autowired
  private SVKCommunicateService vkcommunicateService;
  @Autowired
  private SBasicService basicService;

  @Override
  public String save(AVKCommunicate vkcommunicate, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(vkcommunicate, "vkcommunicate");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = vkcommunicateService.save(
        SVKCommunicateConverter.getInstance().convert(vkcommunicate),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("无法保存招商洽谈记录:", resp.getMessage());
    }
  }

  @Override
  public QueryResult<AVKCommunicate> query(AVKCommunicateQueryParam queryParam)
      throws ThorServiceException {
    SResponse<SQueryResult<SVKCommunicate>> resp = vkcommunicateService
        .query(SVKCommunicateQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return SQueryResultConverter.convert(resp.getBody(), AVKCommunicateConverter.getInstance());
    } else {
      throw new ThorServiceException("无法查询招商洽谈记录：" + resp.getMessage());
    }
  }

  @Override
  public AVKCommunicate get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SVKCommunicate> resp = vkcommunicateService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess()) {
      return AVKCommunicateConverter.getInstance().convert(resp.getBody());
    } else {
      throw new ThorServiceException("无法找到指定ID的招商洽谈记录：" + resp.getMessage());
    }
  }

  @Override
  public void delete(String id, long version, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    SResponse<Void> resp = vkcommunicateService.delete(id, version,
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
    } else {
      throw new ThorServiceException("无法删除指定ID的招商洽谈记录：" + resp.getMessage());
    }
  }

}
